package com.ptit.piclient.tools;

import java.util.ArrayList;

import ptit.androidplayer.R;

import com.ptit.piclient.model.Song;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AllSongAdapter extends ArrayAdapter<Song> {
	private final Context context;
	private final ArrayList<Song> songList;
	public AllSongAdapter(Context c, ArrayList<Song> list){
		super(c, R.layout.list_row,list);
		songList = list;
		context = c;
	}
	
	public static class Holder{
		public ImageView imgThumbnail;
		public TextView txtSongname;
		public TextView txtArtist;		
		public TextView txtDuration;
		
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent){
		View v = convertView;
		Holder holder ;
		if(v == null){
			LayoutInflater vi = (LayoutInflater)
					context.getSystemService(
							Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.list_row, null);
			holder = new Holder();
			holder.imgThumbnail = (ImageView)v.findViewById(R.id.thumbnail);
			holder.imgThumbnail.setImageResource(R.drawable.ic_launcher);			
			holder.txtArtist =  (TextView) v.findViewById(R.id.artist);
			holder.txtSongname =  (TextView) v.findViewById(R.id.title);
			holder.txtDuration =  (TextView) v.findViewById(R.id.duration);			
			v.setTag(holder);
			
		}	else holder = (Holder) v.getTag();
		final Song song = songList.get(position);
		if(song != null){			
			holder.txtArtist.setText(song.getArtist()[0]);
			holder.txtSongname.setText(song.getName());
			holder.txtDuration.setText(song.getFormatedDuration());
		}
		return v;		
	}
}
